package club.banyuan.hrms.servlet;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;
import java.util.Enumeration;

@WebFilter(filterName = "CharacterEncodingFilter", urlPatterns = "/*", initParams = {
        @WebInitParam(name = "name", value = "filter")
})
public class CharacterEncodingFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
//        System.out.println("doFilter");
//
//        // 设置编码方式
//        resp.setContentType("text/plain;charset=utf-8");

        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {
        // 单独获取初始化参数
        String value = config.getInitParameter("name");
        System.out.println("name: " + value);

        // 枚举获取初始化参数
        Enumeration<String> initParameterNames = config.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String name1 = initParameterNames.nextElement();
            String value1 = config.getInitParameter(name1);
            System.out.println(name1 + ": " + value1);
        }
    }

}
